﻿<ResourceDictionary
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Controls="clr-namespace:System.Windows.Controls">

    <BooleanToVisibilityConverter x:Key="boolToVisiblityConverterCollapsed"/>
    <Controls:BoolToVisibilityConverterHidden x:Key="boolToVisiblityConverterHidden"/>
    <Controls:DoubleToThicknessLeftConverter x:Key="doubleToMarginConverter"/>
    <Style TargetType="{x:Type Controls:TreeViewExItem}" x:Key="TreeViewExBaseStyle">
        <Style.Resources>
            <ResourceDictionary />
        </Style.Resources>
        <Setter Property="Background" Value="AliceBlue"/>
        <Setter Property="Control.HorizontalContentAlignment"
                Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}, FallbackValue=Left}" />
        <Setter Property="Control.VerticalContentAlignment"
                Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}, FallbackValue=Center}" />
        <Setter Property="Control.Padding" Value="1" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="FrameworkElement.FocusVisualStyle">
            <Setter.Value>
                <Style TargetType="{x:Type IFrameworkInputElement}">
                    <Style.Resources>
                        <ResourceDictionary />
                    </Style.Resources>
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Rectangle />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:TreeViewExItem}">
                    <StackPanel Background="Transparent" x:Name="stacki">
                        <Border x:Name="border"
						    BorderThickness="1" 
                            Padding="{TemplateBinding Control.Padding}"
                            SnapsToDevicePixels="True"
                            CornerRadius="2" VerticalAlignment="Center" Margin="1,0,1,0">
                            <DockPanel x:Name="ct" >
                                <FrameworkElement DockPanel.Dock="Left" Margin="{Binding Offset, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource doubleToMarginConverter}}"/>
                                <ToggleButton DockPanel.Dock="Left" 
                                    IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:TreeViewExItem}},Path=IsExpanded,FallbackValue=False}"
                                    Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:TreeViewExItem}},Path=HasItems, Converter={StaticResource boolToVisiblityConverterHidden},FallbackValue=Hidden}"
                                    ClickMode="Press" Name="Expander" HorizontalAlignment="Right">
                                    <ToggleButton.Style>
                                        <Style TargetType="{x:Type ToggleButton}">
                                            <Style.Resources>
                                                <ResourceDictionary />
                                            </Style.Resources>
                                            <Setter Property="UIElement.Focusable" Value="False" />
                                            <Setter Property="FrameworkElement.Width" Value="16" />
                                            <Setter Property="FrameworkElement.Height" Value="16" />
                                            <Setter Property="Control.Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                        <Border Padding="5" Background="#00FFFFFF" Width="16" Height="16">
                                                            <Path Fill="#00FFFFFF" Stroke="#FF989898" Name="ExpandPath">
                                                                <Path.Data>
                                                                    <PathGeometry Figures="M0,0L0,6L6,0z" />
                                                                </Path.Data>
                                                                <Path.RenderTransform>
                                                                    <RotateTransform Angle="135" CenterX="3" CenterY="3" />
                                                                </Path.RenderTransform>
                                                            </Path>
                                                        </Border>
                                                        <ControlTemplate.Triggers>
                                                            <Trigger Property="UIElement.IsMouseOver" Value="True">
                                                                <Setter Property="Shape.Stroke" TargetName="ExpandPath">
                                                                    <Setter.Value>
                                                                        <SolidColorBrush>
                                                                            #FF1BBBFA</SolidColorBrush>
                                                                    </Setter.Value>
                                                                </Setter>
                                                                <Setter Property="Shape.Fill" TargetName="ExpandPath">
                                                                    <Setter.Value>
                                                                        <SolidColorBrush>#00FFFFFF</SolidColorBrush>
                                                                    </Setter.Value>
                                                                </Setter>
                                                            </Trigger>
                                                            <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                                <Setter Property="UIElement.RenderTransform"
                                                                TargetName="ExpandPath">
                                                                    <Setter.Value>
                                                                        <RotateTransform Angle="180" CenterX="3" CenterY="3" />
                                                                    </Setter.Value>
                                                                </Setter>
                                                                <Setter Property="Shape.Fill" TargetName="ExpandPath">
                                                                    <Setter.Value>
                                                                        <SolidColorBrush>#FF595959</SolidColorBrush>
                                                                    </Setter.Value>
                                                                </Setter>
                                                                <Setter Property="Shape.Stroke" TargetName="ExpandPath">
                                                                    <Setter.Value>
                                                                        <SolidColorBrush>#FF262626</SolidColorBrush>
                                                                    </Setter.Value>
                                                                </Setter>
                                                            </Trigger>
                                                        </ControlTemplate.Triggers>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </ToggleButton.Style>
                                </ToggleButton>
                                <ContentPresenter x:Name="content"
                                    FocusManager.IsFocusScope="True" 
                                    Content="{TemplateBinding HeaderedContentControl.Header}"
                                    ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                    ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                                    ContentTemplateSelector="{TemplateBinding HeaderedItemsControl.HeaderTemplateSelector}"
                                    ContentSource="Header"
                                    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            </DockPanel>
                        </Border>
                        <ItemsPresenter x:Name="itemsPresenter" Visibility="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource boolToVisiblityConverterCollapsed}, FallbackValue=Hidden}"/>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True" SourceName="border">
                            <Setter Property="Control.Background" TargetName="border"
                                    Value="{Binding BackgroundHovered, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Border.BorderBrush" TargetName="border"
                                    Value="{Binding BorderBrushHovered, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </Trigger>
                        <Trigger Property="Controls:TreeViewExItem.IsSelected" Value="True">
                            <Setter Property="Control.Background" TargetName="border"
                                    Value="{Binding BackgroundSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Border.BorderBrush" TargetName="border"
                                    Value="{Binding BorderBrushSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </Trigger>
                        <Trigger Property="Controls:TreeViewExItem.IsFocused" Value="True">
                            <Setter Property="Border.BorderBrush" TargetName="border"
                                    Value="{Binding BorderBrushFocused, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Control.Background" TargetName="border"
                                    Value="{Binding BackgroundFocused, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="Controls:TreeViewExItem.IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Border.BorderBrush" TargetName="border"
                                    Value="{Binding BorderBrushSelectedHovered, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Control.Background" TargetName="border"
                                    Value="{Binding BackgroundSelectedHovered, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Controls:TreeViewExItem.IsFocused" Value="True" />
                                <Condition Property="Controls:TreeViewExItem.IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Border.BorderBrush" TargetName="border"
                                    Value="{Binding BorderBrushFocusedSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Control.Background" TargetName="border"
                                    Value="{Binding BackgroundFocusedSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </MultiTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition
                                        Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=IsActive, FallbackValue=False}"
                                        Value="False" />
                                <Condition
                                        Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}"
                                        Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Border.BorderBrush" TargetName="border"
                                    Value="{Binding BorderBrushInactive, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Control.Background" TargetName="border"
                                    Value="{Binding BackgroundInactive, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </MultiDataTrigger>

                        <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground">
                                <Setter.Value>
                                    <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEditable" Value="False">
                            <Setter Property="TextElement.Foreground" TargetName="content">
                                <Setter.Value>
                                    <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="Controls:TreeViewExItem.IsCurrentDropTarget" Value="True">
                            <Setter Property="Control.Background"
                                    Value="{Binding BackgroundSelected, RelativeSource={RelativeSource Self}}" />
                            <Setter Property="Border.BorderBrush"
                                    Value="{Binding BorderBrushSelected, RelativeSource={RelativeSource Self}}" />
                        </Trigger>
                        <Trigger Property="Controls:TreeViewExItem.IsEditing" Value="True">
                            <Setter TargetName="content" Property="ContentPresenter.ContentTemplate" 
                                    Value="{Binding TemplateEdit, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}"/>
                            <Setter Property="Padding" Value="0" />
                            <Setter Property="Control.Background"
                                    Value="{Binding BackgroundSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                            <Setter Property="Border.BorderBrush"
                                    Value="{Binding BorderBrushSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:TreeViewExItem}, AncestorLevel=1}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>